<?php

namespace app\common\model\banxi\learn;

use app\common\enums\QuestionTypeEnum;
use app\common\model\common\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 阅读理解答题记录
 */
class LearnReadingArticleAnswer extends BaseModel
{
    use SoftDelete;

    protected $name = 'banxi_learn_reading_article_answer';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;

    protected $append = [
        'answerList',
        'userAnswerList',
        'questionTypeCn',
        'optionList'
    ];

    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'readingArticleId' => 'string',
        'studentId' => 'string',
        'trainerId' => 'string',
        'courseId' => 'string',
        'articleId' => 'string',
        'questionId' => 'string',
        'isCorrect' => 'boolean',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s',
    ];

    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                       ｜
    //-----------------------------------------------------------------------------------

    public function getAnswerListAttr(int|null $value, array $data): array
    {
        return $data['answer'] ? json_decode($data['answer'], true) : [];
    }

    public function getUserAnswerListAttr(int|null $value, array $data): array
    {
        return $data['userAnswer'] ? json_decode($data['userAnswer'], true) : [];
    }

    public function getOptionListAttr(int|null $value, array $data): array
    {
        return $data['option'] ? json_decode($data['option'], true) : [];
    }

    public function getQuestionTypeCnAttr(int|null $value, array $data): string
    {
        $value = $value ?: ($data['questionType'] ?? '');
        return (new  QuestionTypeEnum())->getDescription($value) ?? '';
    }

    //-----------------------------------------------------------------------------------
    //｜                                   处理方法                                       ｜
    //-----------------------------------------------------------------------------------


    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------


}